<?php
namespace Cubemarketing\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\EventManager\EventManagerInterface;

class SalestatsController extends AbstractActionController
{
    protected $storeTable;
    protected $saleHeadTable;
    
    public function setEventManager(EventManagerInterface $events)
    {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/cubemarketing');
        }, 100);
 
        return $this;
    }
    
    public function getStoreTable()
    {
        if (!$this->storeTable) {
            $sm = $this->getServiceLocator();
            $this->storeTable = $sm->get('Cubemarketing\Model\StoreTable');
        }
        return $this->storeTable;
    }
    
    public function getSaleHeadTable()
    {
        if (!$this->saleHeadTable) {
            $sm = $this->getServiceLocator();
            $this->saleHeadTable = $sm->get('Cubemarketing\Model\SaleHeadTable');
        }
        return $this->saleHeadTable;
    }
    
    public function indexAction()
    {
        return new ViewModel(array(
            'stores' => $this->getStoreTable()->fetchAll(),
        ));
    }
    
    public function viewingAction()
    {
        return new ViewModel(array(
            'saleHeads' => $this->getSaleHeadTable()->getSalestats(1),
        ));
    }
}